package com.jml.tt;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @Author: J
 * @DATE: 2023-03-13 11:36
 */
@RestController
public class ACon {

    @RequestMapping("/a")
    public String a() {
        return sendMessage("https://oapi.dingtalk.com/robot/send?access_token=", "Exceptionzxzxzx");
    }


    @Autowired
    private RestTemplate restTemplate;

    public String sendMessage(String dingUrl, String msg) {
        /*
         json_text = {
            "msgtype": "text",
            "at": {
                "atMobiles": [
                    "177*****620"
                ],
                "isAtAll": False  # false : 仅仅@ "at" 对应的的人
            },
            "text": {
                "content": "123：" + text
            }
        }
         */
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("msgtype", "text");
        JSONObject textObject = new JSONObject();
        textObject.put("content", msg);
        jsonObject.put("text", textObject);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> formEntity = new HttpEntity<String>(jsonObject.toJSONString(), headers);
        String resString = restTemplate.postForObject(dingUrl, formEntity, String.class);
        return resString;
    }

}
